package com.gargi.crimepatrol.network;

import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import com.gargi.crimpatrol.utils.Constant;

public class CrimePatrolParser extends Parser {
	
	public Vector m_crimepatrolvector;
	public CrimePatrol m_crimepatroldata;
	
	public boolean m_entrystate = false;
	
	public Vector getCrimePatrolData() {
		return m_crimepatrolvector;
	}
	
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) {
		super.startElement(uri, localName, qName, attributes);
		
		if(qName.equals(Constant.FEED_TAG)) {
			m_crimepatrolvector = new Vector();
		}
		
		if(qName.equals(Constant.ENTRY_TAG)) {
			m_entrystate = true;
			m_crimepatroldata = new CrimePatrol();
		}
		
		if(m_entrystate && attributes.getLength() > 0) {
			parseAttributes(qName, attributes);
		}
	}
	
	private void parseAttributes(String qName, Attributes attributes) {
		
		for(int i = 0; i<attributes.getLength(); i++) {
			
			String name = attributes.getQName(i);
			String value = attributes.getValue(i);
			
			if(qName.equals(Constant.THUMBNAIL_TAG) && name.equals(Constant.URL_TAG)) {
				m_crimepatroldata.setIcon(value);
			}
			
			if(qName.equals(Constant.RSTP_TAG) && name.equals(Constant.URL_TAG)) {
				m_crimepatroldata.setStreamingurl(value);
			}
		}
	}
	
	public void endElement(String uri, String localName,
    		String qName) throws SAXException {
		String chars = getChars();
		
		if(qName.equals(Constant.TITLE_TAG) && m_entrystate && !m_crimepatroldata.isDraft()) {
			m_crimepatroldata.setTitle(chars);
		}else if(qName.equals(Constant.DES_TAG) && m_entrystate) {
			m_crimepatroldata.setDescription(chars);
		}else if(qName.equals(Constant.APP_DRAFT_TAG) && m_entrystate) {
			m_crimepatroldata.setDraft(getDraftStatus(chars));
		}else if(qName.equals(Constant.ENTRY_TAG) && m_entrystate && !m_crimepatroldata.isDraft()) {
			m_crimepatrolvector.addElement(m_crimepatroldata);
		}
	}
	
	private boolean getDraftStatus(String draftChar) {
		if(draftChar.equals("yes")) {
			return true;
		}
		
		return false;
	}
	
}
